/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function encapsulateMessage()
{
    var wrapper = new VanishWrapper();
    wrapper.encapsulateMessage();
}

function decapsulateMessage()
{
    var wrapper = new VanishWrapper();
    wrapper.decapsulateMessage();
}

function decapsulateCurrentMessage()
{
    var wrapper = new VanishWrapper();
    wrapper.decapsulateCurrentMessage();
}

function VanishWrapper()
{

    //var tb = new Thunderbird();
    var vanish = new Vanish();

    VanishWrapper.prototype.decapsulateError = function(msg)
    {
        var str = msg;
        var m = msg.match(/(Got [0-9]+\/[0-9]+ shares)/);
        if (m) {
            str = "Unable to reconstruct your Vanish message (" +
                m[1] +
                ").\nThe message must have self-destructed.";
        }
        alert(str);
    }

    VanishWrapper.prototype.showResult = function(elm, before, after, isEncapsulated)
    {
        return
        (
            function(data, time)
            {

                if (data.decode)
                {
                    data = data.decode();
                }
                var text = before + data + after;
                if (elm)
                {
                    var value = elm.value;
                    var startPos = elm.selectionStart;
                    var endPos = elm.selectionEnd;
                    var chaine = elm.value;
                    elm.value = chaine.substring(0, startPos) + text
                        + chaine.substring(endPos, chaine.length);
                    elm.selectionStart = startPos;
                    elm.selectionEnd = startPos + text.length;
                }
                else
                {
                    window.openDialog('chrome://{appname}/content/showtext.xul',
                    '', 'chrome, dialog, resizable=yes', isEncapsulated,
                    text, time).focus();
                }
            }
        );

    }

    /*
    VanishWrapper.prototype.updateMessage = function(newText)
    {
        var tbcw = new ThunderbirdComposeWindow();
        tbcw.setMessage(newText);
    }
    */

    VanishWrapper.prototype.encapsulateMessage = function()
    {
        var tbcw = new ThunderbirdComposeWindow();
        var message = tbcw.getMessage()

        tbcw.setStatusBar("Preparing...");
        //alert("encapsulate: " + message);

        try {
            var result = vanish.encapsulate
            ( 
                message,
                tbcw.setStatusBar("Encapsulating..."), //this.showResult(elm, "", "", true),
                function()
                {
                    tbcw.setStatusBar("Encapsulated");
                }
            );
            tbcw.setMessage(result);
        } 
        catch (e)
        {
            alert(e);
        }

    }

    VanishWrapper.prototype.encapsulateSelection = function()
    {   
        var bdy = content.document.body;
        try
        {
            var elm = Selection.isEditable();
            vanish.encapsulate
            (
                Selection.get(),
                showResult(elm, "", "", true),
                function()
                {

                }
            );
        }
        catch (e)
        {
            alert(e);
        }
            
    },


    VanishWrapper.prototype.decapsulateMessage = function()
    {

        var tbcw = new ThunderbirdComposeWindow();
        var message = tbcw.getMessage()

        tbcw.setStatusBar("Extracting...");

        //alert('decapsulate Message body : ' + message);
        try
        {
            var vdo = vanish.extractVDO(message);
            if (vdo)
            {               
                var result = vanish.decapsulate
                (
                    vdo[1],
                    tbcw.setStatusBar("Decapsulating..."),//this.showResult(elm, vdo[0], vdo[2], false),
                    function()
                    {
                        tbcw.setStatusBar("Decapsulated");
                    }
                );
                    
                tbcw.setMessage(result);
                //alert("Message:\n" + result);
            }
            else
            {
                alert("You did not select a Vanish message. Make sure you've " +
                    "highlighted at least the entire message which will have " +
                    "a format similar to:\n\n" +
                    "-----BEGIN VANISH MESSAGE-----\n" +
                    "...\n...\n" +
                    "-----END VANISH MESSAGE-----\n");
            }
        }
        catch (e)
        {
            alert(e);
        }

    },

    VanishWrapper.prototype.decapsulateCurrentMessage = function()
    {

        var tbmw = new ThunderbirdMainWindow();
        var message = tbmw.getCurrentReadMessage();

        tbmw.setStatusBar("Extracting...");

        //alert('decapsulate Message body : ' + message);
        try
        {
            var vdo = vanish.extractVDO(message);
            if (vdo)
            {
                
                var result = vanish.decapsulate
                (
                    vdo[1],
                    tbmw.setStatusBar("Decapsulating..."),    //this.showResult(elm, vdo[0], vdo[2], false),
                    function()
                    {

                        tbmw.setStatusBar("Decapsulated");
                    }
                );

                //alert("Result:\n" + vdo[0] + "\n----\n" + vdo[1] + "\n----\n" + vdo[2]);
                alert("Message:\n" + result);
            }
            else
            {
                alert("You did not select a Vanish message. Make sure you've " +
                    "highlighted at least the entire message which will have " +
                    "a format similar to:\n\n" +
                    "-----BEGIN VANISH MESSAGE-----\n" +
                    "...\n...\n" +
                    "-----END VANISH MESSAGE-----\n");
            }
        }
        catch (e)
        {
            alert(e);
        }

    }

    VanishWrapper.prototype.decapsulateSelection = function()
    {      
        var bdy = content.document.body;
        try {
            var elm = Selection.isEditable();
            var data = Selection.get();
            var vdo = vanish.extractVDO(data);
            if (vdo)
            {

                vanish.decapsulate
                (
                    vdo[1],
                    this.showResult(elm, vdo[0], vdo[2], false),
                    function()
                    {
                        
                    }
                );
            }
            else
            {
                alert("You did not select a Vanish message. Make sure you've " +
                    "highlighted at least the entire message which will have " +
                    "a format similar to:\n\n" +
                    "-----BEGIN VANISH MESSAGE-----\n" +
                    "...\n...\n" +
                    "-----END VANISH MESSAGE-----\n");
            }
        }
        catch (e)
        {
            alert(e);
        }

     }
}



